package com.sise.aio和bio和nio.aio.基于aio的聊天室.test2;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author idea
 * @data 2019/1/22
 */
public class AIOServer {

    //定义一个线程池处理，提高效率
    private ExecutorService executorService;

    //服务器通道
    private AsynchronousServerSocketChannel serverSocketChannel;

    public AIOServer(){

    }

    public static void main(String[] args) throws IOException, InterruptedException {
        AIOServer aioServer=new AIOServer();
        aioServer.init(9009);
        while (true){}
    }

    private void init(int port) throws IOException {
        serverSocketChannel=AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));
        System.out.println("此时服务端已经开启！");
        executorService=Executors.newFixedThreadPool(4);
        serverSocketChannel.accept(this,new AIOServerHandle());
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

    public AIOServer setExecutorService(ExecutorService executorService) {
        this.executorService = executorService;
        return this;
    }

    public AsynchronousServerSocketChannel getServerSocketChannel() {
        return serverSocketChannel;
    }

    public AIOServer setServerSocketChannel(AsynchronousServerSocketChannel serverSocketChannel) {
        this.serverSocketChannel = serverSocketChannel;
        return this;
    }
}
